package com.example.kehoobackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.kehoobackend.common.LikeType;
import com.example.kehoobackend.domain.Comment;
import com.example.kehoobackend.domain.Like;
import com.example.kehoobackend.domain.User;
import com.example.kehoobackend.persistance.CommentMapper;
import com.example.kehoobackend.persistance.LikeMapper;
import com.example.kehoobackend.service.CommentService;
import com.example.kehoobackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

//    @Autowired
//    QueryWrapper<Comment> queryWrapper;
//
//    @Autowired
//    QueryWrapper<Like> likeQueryWrapper;

    @Autowired
    LikeMapper likeMapper;

    @Autowired
    UserService userService;

//    @Autowired
//    QueryWrapper<User> userQueryWrapper;

    @Override
    public List<Comment> getNewestComments(Integer from_id,Integer to_id, Integer type) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_id",to_id).eq("type",type).orderByDesc("create_time");
        List<Comment> comments = list(queryWrapper);
        for(Comment comment : comments){
            comment.setLiked(isLike(comment.getComment_id(),from_id));
        }

        return comments;
    }

    @Override
    public List<Comment> getDefaultComments(Integer from_id,Integer to_id, Integer type) {
        //TODO:默认排序算法
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",type).eq("to_id",to_id);

        List<Comment> comments = list(queryWrapper);
        for(Comment comment : comments){
            comment.setLiked(isLike(comment.getComment_id(),from_id));
        }

        return comments;
    }

    @Override
    public boolean like(Integer comment_id,Integer user_id) {
        //存储点赞关系
        Comment comment = getById(comment_id);
        comment.setLiked_count(comment.getLiked_count()+1);
        if(updateById(comment)){
            Like like = new Like();
            like.setUser_id(user_id);
            like.setTarget_id(comment_id);
            like.setType(LikeType.COMMENT);

            //TODO:确定返回值是多少
            likeMapper.insert(like);
            return true;
        }else return false;
    }

    @Override
    public boolean cancelLike(Integer comment_id,Integer user_id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        //删除点赞关系
        Comment comment = getById(comment_id);
        comment.setLiked_count(comment.getLiked_count()-1);
        if(updateById(comment)){
            likeQueryWrapper.eq("user_id",user_id).eq("target_id",comment.getComment_id())
                    .eq("type",LikeType.COMMENT);
            return likeMapper.delete(likeQueryWrapper) == 1;
        }else return false;
    }

    @Override
    public Comment commentOn(Comment comment) {
        if(save(comment)){
            Integer comment_id = comment.getComment_id();
            Comment newComment = getById(comment_id);
            User user = userService.getById(newComment.getUser_id());
            newComment.setUsername(user.getUsername());
            newComment.setResponses(new ArrayList<>());
            return newComment;
        }
        else return null;
    }

    @Override
    public boolean deleteComment(Integer comment_id) {
        return removeById(comment_id);
    }

    @Override
    public boolean isLike(Integer comment_id, Integer user_id) {
        QueryWrapper<Like> likeQueryWrapper = new QueryWrapper<>();
        likeQueryWrapper.eq("user_id",user_id)
                .eq("target_id",comment_id).eq("type", LikeType.COMMENT);
        Like like = likeMapper.selectOne(likeQueryWrapper);
        return like != null;
    }

    @Override
    public List<Comment> getCommentsByLikeDesc(Integer from_id,Integer to_id, Integer type) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("to_id",to_id).eq("type",type)
                .orderByDesc("liked_count");
        List<Comment> comments = list(queryWrapper);

        for(Comment comment : comments){
            comment.setLiked(isLike(comment.getComment_id(),from_id));
        }
        return comments;
    }

    @Override
    public List<Comment> getCommentResponses(List<Comment> comments) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();

        List<Comment> res = new ArrayList<>();
        for(Comment comment : comments){
            //传递用户名
            userQueryWrapper.eq("user_id",comment.getUser_id()).select("username");
            User user = userService.getOne(userQueryWrapper);
            if (user != null)
                comment.setUsername(user.getUsername());

            userQueryWrapper.clear();


            //评论与评论评论
            if(comment.getAt_comment_id() == null){
                //直接评论,并找出对该评论的评论
                queryWrapper.eq("at_comment_id",comment.getComment_id());
                List<Comment> childComments = list(queryWrapper);
                for (Comment child : childComments){
                    //传递用户名
                    userQueryWrapper.eq("user_id",child.getUser_id()).select("username");
                    User user1 = userService.getOne(userQueryWrapper);
                    if (user1 != null)
                        child.setUsername(user1.getUsername());
                    userQueryWrapper.clear();
                }
                comment.setResponses(childComments);
                res.add(comment);
            }

            queryWrapper.clear();
        }
        return res;
    }

}
